package com.my.hzb.order.ctrl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.my.hzb.common.bean.GlobalBean;
import com.my.hzb.common.bean.ResultBean;
import com.my.hzb.common.util.JsonUtil;
import com.my.hzb.entiy.Orders;
import com.my.hzb.order.service.OrderService;

@Controller
@RequestMapping("/order")
public class OrderOptCtrl {

	private static final Logger log = LogManager.getLogger(OrderOptCtrl.class);
	@Autowired
	private OrderService orderService;
	
	
	@RequestMapping("/initOrder")
	@ResponseBody
	public ResultBean initOrder(HttpServletRequest request,String userName,Integer proId){
		ResultBean result = orderService.initOrderInfo("hzb",proId);
		result.setRespCode(ResultBean.RETURN_SUCCESS_CODE);
		result.setRespMsg("ok");
		return result;
	}
	@RequestMapping("/addOrder")
	@ResponseBody
	public ResultBean addOrder(HttpServletRequest request,String userName,Integer adsId,Integer productId
			,Double orderPrice,Integer orderNumber){
		HttpSession session = request.getSession(true);
		log.info("sessionId="+session.getId());
		log.info("sessionId="+session.getId()+"时间："+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date()));
		long start = System.currentTimeMillis();
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		result = orderService.subOrder(userName, adsId, productId, orderPrice, orderNumber);
		log.info("sessionId="+session.getId()+"订单完成消耗时间:"+(System.currentTimeMillis()-start));
		return result;
	}
	@RequestMapping("/payOrder")
	@ResponseBody
	public ResultBean payOrder(HttpServletRequest request,String orderNo){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		try{
			Orders order = new Orders();
			order.setOrderNo(orderNo);
			order.setStatus(1);
			List<Orders> list = orderService.find(order,null);
			if(null!=list&&list.size()>0){
				order = list.get(0);
				order.setStatus(2);
				//减掉库存
				orderService.update(order);
				result.setRespCode(ResultBean.RETURN_SUCCESS_CODE);
				result.setReturnStr(GlobalBean.Url.user+"main");
				result.setRespMsg("ok");
				return result;
			}
		}catch(Exception e){
			log.error("支付订单失败",e);
		}
		return result;
	}
	@RequestMapping("/list")
	@ResponseBody
	public ResultBean list(HttpServletRequest request,String userName,Integer status){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		Orders order = new Orders();
		if(null!=userName){
			order.setUserName(userName);
		}
		if(null!=status){
			order.setStatus(status);
		}
		List<Orders> list = orderService.find(order,null);
		if(null!=result){
			result.setReturnList(new ArrayList<Object>(list));
		}
		result.setRespCode(ResultBean.RETURN_SUCCESS_CODE);
		result.setRespMsg("ok");
		return result;
	}
	@RequestMapping("/listDef")
	@ResponseBody
	public ResultBean listDef(HttpServletRequest request,String userName){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		Orders order = new Orders();
		if(null!=userName){
			order.setUserName(userName);
		}
		List<Orders> list = orderService.find(order,null);
		if(null!=result){
			result.setReturnList(new ArrayList<Object>(list));
			result.setReturnStr(JsonUtil.g.toJson(list.get(0)));
		}
		result.setRespCode(ResultBean.RETURN_SUCCESS_CODE);
		result.setRespMsg("ok");
		return result;
	}
	
	
	@RequestMapping("/count")
	@ResponseBody
	public ResultBean count(HttpServletRequest request,Orders order){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		try{
			result.setReturnStr(orderService.countByExample(order)+"");
			result.setRespCode(ResultBean.RETURN_SUCCESS_CODE);
			result.setRespMsg("ok");
		}catch(Exception e){
			log.error("查询订单数量失败",e);
		}
		return result;
	}
	@RequestMapping("/countPrice")
	@ResponseBody
	public ResultBean countPrice(HttpServletRequest request,Orders order){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		try{
			result.setReturnStr(orderService.countPrice(order)+"");
			result.setRespCode(ResultBean.RETURN_SUCCESS_CODE);
			result.setRespMsg("ok");
		}catch(Exception e){
			log.error("查询订单数量失败",e);
		}
		return result;
	}
	
}
